package com.example.wan1.view.collect;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.utils.ColorTheme;
import com.example.wan1.utils.StatusBarUtil;
import com.example.wan1.utils.ToolsUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.android.support.DaggerAppCompatActivity;

import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

/***
 * 我的收藏
 */
public class CollectActivity extends DaggerAppCompatActivity {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.search)
    ImageView mSearch;
    @BindView(R.id.collect_tab)
    TabLayout mCollectTab;
    @BindView(R.id.collect_vp)
    ViewPager mCollectVp;
    @BindView(R.id.collect_tbar)
    Toolbar mToolbar;
    @Inject
    List<Fragment> mFragments;
    @Inject
    List<String> mTitles;
    @Inject
    SubPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ColorTheme theme = new ColorTheme(this);
        theme.loadTheme();
        setContentView(R.layout.activity_collect);
        ButterKnife.bind(this);

        initFragment();
        initView();
        initTbar();
    }

    private void initView() {
        mTitle.setText(getString(R.string.collect));
        mSearch.setVisibility(View.GONE);

        mAdapter = new SubPagerAdapter(getSupportFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mAdapter.setData(mFragments, mTitles);
        mCollectVp.setAdapter(mAdapter);
        mCollectTab.setupWithViewPager(mCollectVp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mCollectTab.setElevation(0);
        }
        mCollectTab.removeAllTabs();
        for (int i = 0; i < mTitles.size(); i++) {
            mCollectTab.addTab(mCollectTab.newTab().setText(mTitles.get(i)));
        }

        int color = ToolsUtils.getThemeColor(this);
        if (color != 0) {
            mToolbar.setBackgroundColor(color);
            StatusBarUtil.setColor(this, color, 0);
            mCollectTab.setSelectedTabIndicatorColor(color);
            mCollectTab.setTabTextColors(Color.parseColor("#333333"), color);
        }
        if (ToolsUtils.getNightMode(this))
            StatusBarUtil.setTransparent(this);

        //导航栏着色
        if (ToolsUtils.isOpenNavigationBar(this) && !ToolsUtils.getNightMode(this)) {
            Window window = getWindow();
            /*getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);*/
            if (ToolsUtils.getThemeColor(this) != 0)
                window.setNavigationBarColor(ToolsUtils.getThemeColor(this));
            else
                window.setNavigationBarColor(ToolsUtils.getAttrValue(this));
        }

        String type = getIntent().getStringExtra("type");
        if (TextUtils.equals(type, "my_collect")) {
            mCollectVp.setCurrentItem(0);
        } else {
            mCollectVp.setCurrentItem(2);
        }
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new PerCollectFragment().newInstance(getString(R.string.collect_article_title)));
        mFragments.add(new PerCollectFragment().newInstance(getString(R.string.collect_web_title)));
        mFragments.add(new PerCollectFragment().newInstance(getString(R.string.collect_share_title)));

        mTitles = new ArrayList<>();
        mTitles.add(getString(R.string.collect_article_title));
        mTitles.add(getString(R.string.collect_web_title));
        mTitles.add(getString(R.string.collect_share_title));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initTbar() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
